import 'dart:convert';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:macless_haystack/item_management/item_creation.dart';
import 'package:macless_haystack/item_management/item_file_import.dart';
import 'package:macless_haystack/item_management/item_import.dart';
import 'dart:io';

class NewKeyAction extends StatelessWidget {
  /// If the action button is small.
  final bool mini;

  /// Displays a floating button used to access the accessory creation menu.
  ///
  /// A new accessory can be created or an existing one imported manually.
  const NewKeyAction({
    super.key,
    this.mini = false,
  });

  /// Display a bottom sheet with creation options.
  void showCreationSheet(BuildContext context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return SafeArea(
            child: ListView(
              shrinkWrap: true,
              children: [
                ListTile(
                  title: const Text('导入定位标签'),
                  leading: const Icon(Icons.import_export),
                  onTap: () {
                    Navigator.pushReplacement(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const AccessoryImport()),
                    );
                  },
                ),
                ListTile(
                  title: const Text('导入 JSON 文件'),
                  leading: const Icon(Icons.description),
                  onTap: () async {
                    FilePickerResult? result =
                        await FilePicker.platform.pickFiles(
                      allowMultiple: true,
                      type: FileType.custom,
                      allowedExtensions: ['json'],
                      dialogTitle: '选择定位标签配置文件',
                    );

                    if (result != null) {
                      if (result.files.length == 1) {
                        var uploadfile = result.files.single.bytes;
                        if (uploadfile != null && context.mounted) {
                          Navigator.pushReplacement(
                              context,
                              MaterialPageRoute(
                                builder: (context) =>
                                    ItemFileImport(bytes: uploadfile),
                              ));
                        } else if (result.paths.isNotEmpty) {
                          String? filePath = result.paths[0];
                          if (filePath != null) {
                            var fileAsBytes =
                                await File(filePath).readAsBytes();
                            if (context.mounted) {
                              Navigator.pushReplacement(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) =>
                                        ItemFileImport(bytes: fileAsBytes),
                                  ));
                            }
                          }
                        }
                      } else {
                        List<dynamic> contentList = [];
                        for (var i = 0; i < result.files.length; i++) {
                          var uploadfile = result.files.elementAt(i).bytes;
                          if (uploadfile == null) {
                            String? filePath = result.files.elementAt(i).path;
                            if (filePath != null) {
                              uploadfile = await File(filePath).readAsBytes();
                            }
                          }
                          if (uploadfile != null) {
                            String encodedContent = utf8.decode(uploadfile);
                            List<dynamic> content = jsonDecode(encodedContent);
                            contentList.addAll(content);
                          }
                        }
                        var contentListJson = jsonEncode(contentList);
                        if (context.mounted) {
                          Navigator.pushReplacement(
                              context,
                              MaterialPageRoute(
                                builder: (context) => ItemFileImport(
                                    bytes: utf8.encode(contentListJson)),
                              ));
                        }
                      }
                    }
                  },
                ),
                ListTile(
                  title: const Text('创建新的定位标签'),
                  leading: const Icon(Icons.add_box),
                  onTap: () {
                    Navigator.pushReplacement(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const AccessoryGeneration()),
                    );
                  },
                ),
              ],
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      mini: mini,
      heroTag: null,
      onPressed: () {
        showCreationSheet(context);
      },
      tooltip: '添加定位标签',
      child: const Icon(Icons.add),
    );
  }
}
